<template>
    <div class="system-resource-container">
        <el-card shadow="hover" class="mb10" v-auth="'system:resource:list'">
            <el-form :inline="true" :model="search">
                <el-form-item>
                    <el-input v-model="search.name" placeholder="请输入资源名称" prefix-icon="el-icon-search"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-input v-model="search.module" placeholder="请输入模块名称" prefix-icon="el-icon-search"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-input v-model="search.path" placeholder="请输入资源路径" prefix-icon="el-icon-search"></el-input>
                </el-form-item>
				<el-form-item>
					<el-input v-model="search.perms" placeholder="请输入权限字符" prefix-icon="el-icon-search"></el-input>
				</el-form-item>
				<el-form-item>
					<el-select v-model="search.belong" placeholder="请选择资源归属" clearable class="w100">
						<el-option label="系统资源" value="AUTH"></el-option>
						<el-option label="开放资源" value="OPEN"></el-option>
					</el-select>
				</el-form-item>
                <el-form-item>
                    <el-button icon="el-icon-search" type="primary" @click="onTableSearch()">查询</el-button>
                    <el-button icon="el-icon-refresh" @click="onResetSearch()">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>
        <el-card shadow="hover">
            <el-button v-auth="'system:resource:add'" type="primary" class="ml10" icon="el-icon-document-add" @click="onOpenAddResource()">新增</el-button>
            <el-button v-auth="'system:resource:edit'" type="primary" class="ml10" icon="el-icon-edit" @click="onOpenEditResource()" :disabled="tableData.selections.length != 1">修改</el-button>
            <el-button v-auth="'system:resource:remove'" type="primary" class="ml10" icon="el-icon-delete" @click="onTabelRowsDel()" :disabled="tableData.selections.length <= 0">删除</el-button>
            <el-table ref="tableResourceRef" :data="tableData.data" style="width: 100%" row-key="id" :tree-props="{ children: 'children'}">
                <el-table-column type="selection" align="cneter" width="50" show-overflow-tooltip></el-table-column>
                <el-table-column type="index" align="cneter" width="50" show-overflow-tooltip></el-table-column>
				<el-table-column label="资源名称" prop="name" min-width="120" show-overflow-tooltip></el-table-column>
                <el-table-column label="模块名称" prop="module" min-width="120" show-overflow-tooltip></el-table-column>
                <el-table-column label="资源路径" prop="path" min-width="180" show-overflow-tooltip></el-table-column>
				<el-table-column label="权限字符" prop="perms" min-width="90" show-overflow-tooltip>
					<template #default="scope">
						<span v-if="scope.row.type == 'GROUP'" class="color-success">{{ scope.row.perms }}</span>
						<span v-else-if="scope.row.type == 'RESOURCE'" class="color-warning">{{ scope.row.perms }}</span>
						<span v-else class="color-primary">{{ scope.row.perms }}</span>
					</template>
				</el-table-column>
				<el-table-column label="资源类型" show-overflow-tooltip width="80" align="center">
					<template #default="scope">
						<span v-if="scope.row.type == 'GROUP'" class="color-success">分组</span>
						<span v-else-if="scope.row.type == 'RESOURCE'" class="color-warning">资源</span>
						<span v-else class="color-danger">未知类型</span>
					</template>
				</el-table-column>
				<el-table-column label="资源归属" show-overflow-tooltip width="100" align="center">
					<template #default="scope">
						<span v-if="scope.row.belong == 'AUTH'" class="color-primary">系统资源</span>
                        <span v-else-if="scope.row.belong == 'COMMON'" class="color-success">公共资源</span>
						<span v-else-if="scope.row.belong == 'OPEN'" class="color-warning">开放资源</span>
						<span v-else class="color-danger">未知归属</span>
					</template>
				</el-table-column>
				<!-- <el-table-column label="状态" show-overflow-tooltip width="80" align="center">
					<template #default="scope">
						<span v-if="scope.row.status == '0'" class="color-primary">正常</span>
						<span v-else-if="scope.row.status == '1'" class="color-info">停用</span>
						<span v-else class="color-danger">删除</span>
					</template>
				</el-table-column> -->
				<!-- <el-table-column label="排序" prop="orderNum" width="80" align="center" show-overflow-tooltip></el-table-column> -->
				<!-- <el-table-column label="创建时间" prop="createTime" width="180" show-overflow-tooltip></el-table-column> -->
                <el-table-column label="操作" show-overflow-tooltip width="125" v-auths="['system:resource:edit', 'system:resource:remove']">
                    <template #default="scope">
                        <el-button v-auth="'system:resource:edit'" type="primary" link @click="onOpenEditResource(scope.row)">修改</el-button>
                        <el-button v-auth="'system:resource:remove'" type="primary" link @click="onTabelRowDel(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </el-card>
        <AddResource ref="addResourceRef" @submit="onTableSearch"/>
        <EditResource ref="editResourceRef" @submit="onTableSearch"/>
    </div>
</template>

<script lang="ts">
import { ref, toRefs, reactive, onMounted } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import AddResource from '/@/views/system/resource/addResource.vue';
import EditResource from '/@/views/system/resource/editResource.vue';
import { getResourceList, delResource } from '/@/api/system/resource/index';
import { lineToTreeData } from '/@/utils/commonFunction';
export default {
    name: 'system/resource',
    components: { AddResource, EditResource },
    setup() {
        const tableResourceRef = ref();
        const addResourceRef = ref();
        const editResourceRef = ref();
        const state = reactive({
            search:{
                name: undefined,
                module: undefined,
                perms: undefined,
                path: undefined,
				belong: undefined,
            },
            tableData: {
                selections: [],//选中的行数据
                data: [],
                total: 0,
                loading: false
            }
        });

        const loadTableData = (params?: object) => {
            getResourceList(Object.assign(state.search, params)).then((res: any) => {
                if(res.code == 0){
                    state.tableData.data = lineToTreeData(res.data);
                    state.tableData.total = res.total;
                }
            });
        }

        //查询
        const onTableSearch = () => {
            loadTableData(state.search);
        }
        //重置
        const onResetSearch = () => {
            state.search.name = undefined;
            state.search.module = undefined;
            state.search.perms = undefined;
            state.search.path = undefined;
			state.search.belong = undefined;
            loadTableData(state.search);
        }
        // 打开新增弹窗
        const onOpenAddResource = (row: object) => {
            addResourceRef.value.openDialog(row);
        };
        // 打开编辑弹窗
        const onOpenEditResource = (row: object) => {
            row = row || state.tableData.selections[0];
            editResourceRef.value.openDialog(row);
        };
        // 删除当前行
        const onTabelRowDel = (row: object) => {
            ElMessageBox.confirm('确定删除该条开放资源信息吗？', '提示', {
                confirmButtonText: '删除',
                cancelButtonText: '取消',
                type: 'warning',
            }).then(() => {
                delResource([row.id]).then((res) => {
                    if(res.code === 0){
                        ElMessage.success('删除成功');
                        loadTableData();
                    }
                });
            }).catch(() => {});
        };
        // 页面加载时
        onMounted(() => {
            loadTableData();
        });
        return {
            tableResourceRef,
            addResourceRef,
            editResourceRef,
            onTableSearch,
            onResetSearch,
            onOpenAddResource,
            onOpenEditResource,
            onTabelRowDel,
            ...toRefs(state),
        };
    },
};
</script>